安装Hive

解压 hive 并添加环境变量

cd /opt
tar zxvf apache-hive-3.1.3-bin.tar.gz
mv apache-hive-3.1.3-bin hive
//创建hive.sh
cat > /etc/profile.d/hive.sh << EOF
export HIVE_HOME=/opt/hive
export PATH=$HIVE_HOME/bin:$PATH
EOF
//应用环境变量
source /etc/profile

修改 hive 文件夹权限为 hadoop

chown -R hadoop:hadoop /opt/hive

配置 hive

cd /opt/hive/conf

添加 hive 配置

cat > hive-site.xml << EOF
<configuration>
      <property>
        <name>javax.jdo.option.ConnectionURL</name>
        <value>jdbc:mysql://192.168.122.24:3306/hive?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=UTF-8</value>
      </property>
      <property>
        <name>javax.jdo.option.ConnectionDriverName</name>
        <value>com.mysql.jdbc.Driver</value>
      </property>
        <property>
        <name>hive.druid.metadata.db.type</name>
        <value>mysql</value>
      </property>
      <property>
        <name>hive.druid.metadata.uri</name>
        <value>jdbc:mysql://192.168.122.24:3306</value>
      </property>
      <property>
        <name>javax.jdo.option.ConnectionUserName</name>
        <value>hive</value>
      </property>
      <property>
        <name>javax.jdo.option.ConnectionPassword</name>
        <value>hivepassword</value>
      </property>
      <property>
        <name>hive.metastore.warehouse.dir</name>
        <value>/user/hive/warehouse</value>
      <description>location of default database for the warehouse</description>
      </property>
      <property>
        <name>hive.metastore.uris</name>
        <value>thrift://192.168.122.24:9083</value>
      </property>
      <property>
        <name>hive.metastore.schema.verification</name>
        <value>false</value>
      </property>
      <property>
        <name>hive.server2.enable.doAs</name>
        <value>FALSE</value>
        <description>
        Setting this property to true will have HiveServer2 execute
        Hive operations as the user making the calls to it.
        </description>
      </property> 
      <property>
        <name>hive.metastore.schema.verification.record.version</name>
        <value>false</value>
      </property>
      <property>
        <name>hive.security.authorization.enabled</name>
        <value>false</value>
      </property>
      <property>
        <name>hive.metastore.port</name>
        <value>9083</value>
      </property>
      <property>
        <name>hive.server2.thrift.bind.host</name>
        <value>0.0.0.0</value>
      </property>
      <property>
        <name>hive.server2.thrift.port</name>
        <value>10000</value>
      </property>
      <property>
        <name>hive.server2.transport.mode</name>
        <value>binary</value>
      </property>
      <property>
        <name>hive.metastore.transactional.event.listeners</name>
        <value>org.apache.hive.hcatalog.listener.DbNotificationListener</value>
      </property>
      <property>
        <name>hive.metastore.event.db.notification.api.auth</name>
        <value>false</value>
      </property>
      <property>
        <name>hive.server2.enable.impersonation</name>
        <value>false</value>
      </property>
      <!-- 开启事务支持 -->
      <property>
        <name>hive.support.concurrency</name>
        <value>true</value>
      </property>
      <!-- 事务管理器 -->
      <property>
        <name>hive.txn.manager</name>
        <value>org.apache.hadoop.hive.ql.lockmgr.DbTxnManager</value>
      </property>
      <!-- 启用动态分区 -->
      <property>
        <name>hive.exec.dynamic.partition.mode</name>
        <value>nonstrict</value>
      </property>
      <!-- 事务表默认存储格式为ORC -->
      <property>
        <name>hive.default.fileformat</name>
        <value>ORC</value>
      </property>
      <property>
        <name>hive.exec.mode.local.auto</name>
        <value>true</value>
      </property>
      <property>
        <name>hive.server2.authentication</name>
        <value>NONE</value> <!-- 允许匿名访问 -->
      </property>
      <property>
       <name>hive.execution.engine</name>
       <value>mr</value>
      </property>
      <property>
       <name>hive.input.format</name>
       <value>org.apache.hadoop.hive.ql.io.HiveInputFormat</value>
      </property>
    </configuration>
EOF

配置 hive 运行日志

cat >> conf/hive-log4j2.properties << EOF
rootLogger.level = INFO
rootLogger.appenderRef.stdout.ref = console
rootLogger.appenderRef.rolling.ref = file
  
appender.console.name = console
appender.console.type = Console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{ISO8601} %-5p [%t] %c: %m%n

appender.rolling.name = file
appender.rolling.type = RollingFile
appender.rolling.fileName = /opt/hive/hiveserver2.log
appender.rolling.filePattern = /opt/hive/hiveserver2-%d{MM-dd-yyyy}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{ISO8601} %-5p [%t] %c: %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true

logger.query.level = INFO
logger.query.name = org.apache.hive
EOF

添加 mysql 用户

//进入mysql
mysql -u root -p
//创建hive数据库
CREATE DATABASE hive;
//创建hive用户
CREATE USER 'hive'@'%' IDENTIFIED BY 'your_password';
//授予权限
GRANT ALL PRIVILEGES ON hive.* TO 'hive'@'%';
//刷新权限
flush privileges;

将 MySQL JDBC 驱动加入 hive 的 lib 包中


cp /opt/mysql-connector-java-8.0.28.jar /opt/hive/lib/

初始化 Hive MetaStore 数据库


schematool -dbType mysql -initSchema

启动 Hive metastore


//启动一个tmux终端后台运行metastore

tmux new -s metastore

hive --service metastore

启动 Hiveserver2


//启动一个tmux终端后台运行hiveserver2

hive --service hiveserver2

验证本机 hive 环境

[hadoop@localhost opt]$ hive -e "show databases"
which: no hbase in (/bin:/sbin:/root/.nvm/versions/node/v12.22.12/bin:/usr/local/maven/bin:/opt/mysql/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64/bin:/opt/hive/bin:/opt/hadoop/bin:/opt/hadoop/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hive/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
2025-01-13T21:43:36,908 INFO [main] org.apache.hadoop.hive.conf.HiveConf: Found configuration file file:/opt/hive/conf/hive-site.xml
2025-01-13T21:43:37,273 WARN [main] org.apache.hadoop.hive.conf.HiveConf: HiveConf of name hive.server2.enable.impersonation does not exist
2025-01-13T21:43:37,482 WARN [main] org.apache.hadoop.hive.conf.HiveConf: HiveConf of name hive.server2.enable.impersonation does not exist
Hive Session ID = 8ba4dddf-ee09-49e2-b10b-370d4981aac6
2025-01-13T21:43:38,071 INFO [main] SessionState: Hive Session ID = 8ba4dddf-ee09-49e2-b10b-370d4981aac6

Logging initialized using configuration in file:/opt/hive/conf/hive-log4j2.properties Async: true
2025-01-13T21:43:38,169 INFO [main] SessionState:
Logging initialized using configuration in file:/opt/hive/conf/hive-log4j2.properties Async: true
2025-01-13T21:43:39,601 INFO [main] org.apache.hadoop.hive.ql.session.SessionState: Created HDFS directory: /tmp/hive/hadoop/8ba4dddf-ee09-49e2-b10b-370d4981aac6
2025-01-13T21:43:39,663 INFO [main] org.apache.hadoop.hive.ql.session.SessionState: Created local directory: /tmp/hadoop/8ba4dddf-ee09-49e2-b10b-370d4981aac6
2025-01-13T21:43:39,705 INFO [main] org.apache.hadoop.hive.ql.session.SessionState: Created HDFS directory: /tmp/hive/hadoop/8ba4dddf-ee09-49e2-b10b-370d4981aac6/_tmp_space.db
2025-01-13T21:43:39,749 INFO [main] org.apache.hadoop.hive.conf.HiveConf: Using the default value passed in for log id: 8ba4dddf-ee09-49e2-b10b-370d4981aac6
2025-01-13T21:43:39,750 INFO [main] org.apache.hadoop.hive.ql.session.SessionState: Updating thread name to 8ba4dddf-ee09-49e2-b10b-370d4981aac6 main
2025-01-13T21:43:39,859 WARN [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.conf.HiveConf: HiveConf of name hive.server2.enable.impersonation does not exist
2025-01-13T21:43:40,956 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.metastore.HiveMetaStoreClient: Trying to connect to metastore with URI thrift://192.168.122.24:9083
2025-01-13T21:43:41,017 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.metastore.HiveMetaStoreClient: Opened a connection to metastore, current connections: 1
2025-01-13T21:43:41,072 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.metastore.HiveMetaStoreClient: Connected to metastore.
2025-01-13T21:43:41,072 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.metastore.RetryingMetaStoreClient: RetryingMetaStoreClient proxy=class org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient ugi=hadoop (auth:SIMPLE) retries=1 delay=1 lifetime=0
2025-01-13T21:43:41,302 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.conf.HiveConf: Using the default value passed in for log id: 8ba4dddf-ee09-49e2-b10b-370d4981aac6
Hive Session ID = 9acbeb8d-f253-4ff9-87a2-49b53e9f4cf7
2025-01-13T21:43:41,304 INFO [pool-7-thread-1] SessionState: Hive Session ID = 9acbeb8d-f253-4ff9-87a2-49b53e9f4cf7
2025-01-13T21:43:41,371 INFO [pool-7-thread-1] org.apache.hadoop.hive.ql.session.SessionState: Created HDFS directory: /tmp/hive/hadoop/9acbeb8d-f253-4ff9-87a2-49b53e9f4cf7
2025-01-13T21:43:41,377 INFO [pool-7-thread-1] org.apache.hadoop.hive.ql.session.SessionState: Created local directory: /tmp/hadoop/9acbeb8d-f253-4ff9-87a2-49b53e9f4cf7
2025-01-13T21:43:41,397 INFO [pool-7-thread-1] org.apache.hadoop.hive.ql.session.SessionState: Created HDFS directory: /tmp/hive/hadoop/9acbeb8d-f253-4ff9-87a2-49b53e9f4cf7/_tmp_space.db
2025-01-13T21:43:41,441 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.Driver: Compiling command(queryId=hadoop_20250113214341_50e76e03-0c40-42e3-900b-55ea4e9d5d68): show databases
2025-01-13T21:43:41,503 INFO [pool-7-thread-1] org.apache.hadoop.hive.ql.metadata.HiveMaterializedViewsRegistry: Materialized views registry has been initialized
2025-01-13T21:43:42,628 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.Driver: Semantic Analysis Completed (retrial = false)
2025-01-13T21:43:42,678 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.Driver: Returning Hive schema: Schema(fieldSchemas:[FieldSchema(name:database_name, type:string, comment:from deserializer)], properties:null)
2025-01-13T21:43:42,772 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.exec.ListSinkOperator: Initializing operator LIST_SINK[0]
2025-01-13T21:43:42,783 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.Driver: Completed compiling command(queryId=hadoop_20250113214341_50e76e03-0c40-42e3-900b-55ea4e9d5d68); Time taken: 1.389 seconds
2025-01-13T21:43:42,783 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.reexec.ReExecDriver: Execution #1 of query
2025-01-13T21:43:42,784 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.Driver: Executing command(queryId=hadoop_20250113214341_50e76e03-0c40-42e3-900b-55ea4e9d5d68): show databases
2025-01-13T21:43:42,796 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.Driver: Starting task [Stage-0:DDL] in serial mode
2025-01-13T21:43:42,808 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] hive.ql.exec.DDLTask: results : 1
OK
2025-01-13T21:43:42,848 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.Driver: Completed executing command(queryId=hadoop_20250113214341_50e76e03-0c40-42e3-900b-55ea4e9d5d68); Time taken: 0.064 seconds
2025-01-13T21:43:42,849 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.Driver: OK
2025-01-13T21:43:42,876 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.conf.Configuration.deprecation: mapred.input.dir is deprecated. Instead, use mapreduce.input.fileinputformat.inputdir
2025-01-13T21:43:42,949 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.mapred.FileInputFormat: Total input files to process : 1
default
2025-01-13T21:43:42,990 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.exec.ListSinkOperator: RECORDS_OUT_INTERMEDIATE:0, RECORDS_OUT_OPERATOR_LIST_SINK_0:1,
Time taken: 1.459 seconds, Fetched: 1 row(s)
2025-01-13T21:43:42,999 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.conf.HiveConf: Using the default value passed in for log id: 8ba4dddf-ee09-49e2-b10b-370d4981aac6
2025-01-13T21:43:42,998 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] CliDriver: Time taken: 1.459 seconds, Fetched: 1 row(s)
2025-01-13T21:43:42,999 INFO [8ba4dddf-ee09-49e2-b10b-370d4981aac6 main] org.apache.hadoop.hive.ql.session.SessionState: Resetting thread name to main
2025-01-13T21:43:42,999 INFO [main] org.apache.hadoop.hive.conf.HiveConf: Using the default value passed in for log id: 8ba4dddf-ee09-49e2-b10b-370d4981aac6
2025-01-13T21:43:43,044 INFO [main] org.apache.hadoop.hive.ql.session.SessionState: Deleted directory: /tmp/hive/hadoop/8ba4dddf-ee09-49e2-b10b-370d4981aac6 on fs with scheme hdfs
2025-01-13T21:43:43,045 INFO [main] org.apache.hadoop.hive.ql.session.SessionState: Deleted directory: /tmp/hadoop/8ba4dddf-ee09-49e2-b10b-370d4981aac6 on fs with scheme file
2025-01-13T21:43:43,052 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStoreClient: Closed a connection to metastore, current connections: 0